import {
    TreeActionType,
    ICtrlDataAction,
    ICtrlEvent,
    ITreeAbility,
    ITreeControllerParams,
    ITreeModel,
    ITreeStore,
    IParam,
    ITreeController,
} from '@/core';
import { QXEvent } from 'qx-util';
import { PropType } from 'vue';
import {
    getCtrlClassNames,
    useCtrlAction,
    useCtrlBasic,
    useCtrlControllerParams,
    useCtrlProps,
} from '../use-ctrl';

/**
 * 树输入参数
 *
 * @export
 * @return {*}
 */
export function useTreeProps() {
    return {
        ...useCtrlProps(),
        openView: Function,
        newView: Function,
        actions: Object as PropType<ICtrlDataAction>,
        selectFirstDefault: Boolean,
        singleSelect: {
			type: Boolean,
			default: true,
		},
        selections: {
			type: Array as PropType<IParam[]>,
			default: [],
		},
    };
}

/**
 * 树基础参数
 *
 * @export
 * @param {ITreeModel} model
 * @param {IParam} props
 * @return {*}
 */
export function useTreeBasic(model: ITreeModel, props: IParam) {
    const getClassNames = (props: IParam) => {
        const names = getCtrlClassNames(model, props);
        return names;
    };
    return {
        ...useCtrlBasic(model, props),
        slotKeys: Object.keys(useSlots()),
        getClassNames,
    };
}

/**
 * 树部件行为
 *
 * @export
 */
export function useTreeAction(controller: ITreeController<ITreeStore,ITreeAbility>) {
    const handleSelectionChange = (selections: IParam[]) => {
        controller.handleSelectionChange(selections);
    }
    
    const handleExpandChange = (expandedKeys: string[]) => {
        controller.handleExpandChange(expandedKeys);
    }
    
    const handleValueChange = (item: IParam) => {
        controller.handleValueChange(item);
    }
    
    const handleContextMenuItemClick = (node: IParam, args: IParam) => {
        controller.handleContextMenuItemClick(node, args);
    }
    return {
        ...useCtrlAction(controller),
        handleSelectionChange,
        handleExpandChange,
        handleValueChange,
        handleContextMenuItemClick
    };
}

/**
 * 树部件控制器
 *
 * @export
 */
export function useTreeControllerParams<
    S extends ITreeStore,
    M extends ITreeModel
>(
    model: M,
    props: any,
    evt: QXEvent<ICtrlEvent<TreeActionType, ITreeAbility>>,
    otherParams: IParam = {},
): ITreeControllerParams<TreeActionType, ITreeAbility> {
    return {
        ...useCtrlControllerParams<S, M>(model, props, evt, otherParams),
        actions: props.actions,
        selections: props.selections,
        singleSelect: props.singleSelect,
        selectFirstDefault: props.selectFirstDefault,
        openView: props.openView,
        newView: props.newView,
    };
}
